﻿using Luna小店.DTO;
using Luna小店.service;
using Luna小店.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.UC.任务
{
    public partial class Frm样品面料任务 : DevExpress.XtraEditors.XtraForm
    {
        public DataRow dataRow { get; set; }
        public 面料样品DTO dto面料样品 { get; set; }
        public ImageUtil imageUtil { get; set; }
        public Frm样品面料任务()
        {
            InitializeComponent();
        }
        public Frm样品面料任务(String orderid,面料样品DTO dto)
        {
            this.dto面料样品 = dto;
            this.dto面料样品.ORDER_ID = orderid;
            this.imageUtil = new ImageUtil(this.dto面料样品.imageIOStream, this.dto面料样品.SAMPLE_PIC);
            InitializeComponent();
            this.LoadControls(this.dto面料样品);
        }

        private void Frm样品面料任务_Load(object sender, EventArgs e)
        {
            this.gridControl样衣面料.DataSource = FabricService.GetDTBySampleOrderID(this.dto面料样品.ORDER_ID);
            this.dataRow = FabricService.GetDRBySampleOrderID(this.dto面料样品.ORDER_ID);
        }

        private void LoadControls(面料样品DTO dto)
        {
            this.pictureBoxImage.Image = dto.SAMPLE_PIC;
            this.textBox规格.Text = dto.UNIT_CONSUMPTION;
            this.richTextBox使用部位.Text = dto.REMARK;
        }

        private void barButtonItem上传图片_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.imageUtil = FileService.updateTempalteFile(MIMEConst.IMAGE_MIME, this.imageUtil);
            this.pictureBoxImage.Image = this.imageUtil.image;
        }

        private void barButtonItem保存_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            String file_name = @"" + this.dto面料样品.ORDER_ID + "_" + System.Guid.NewGuid().ToString("N") + ".doc";
            this.richEditControl辅配料.SaveDocument(file_name, DevExpress.XtraRichEdit.DocumentFormat.Doc);
            Byte[] byteArray = FileBinaryConvertHelper.File2Bytes(file_name);
            String str = Convert.ToBase64String(byteArray);
            FabricService.Updata辅配料文档(str, this.dto面料样品.ORDER_ID);
            File.Delete(file_name);

            this.dto面料样品 = new 面料样品DTO(this.dto面料样品.ORDER_ID, this.dataRow["MATERIAL_ID"].ToString(), this.textBox规格.Text.Trim(), this.imageUtil, this.richTextBox使用部位.Text.Trim());
            if (FabricService.SaveInMTM样衣面料任务(this.dto面料样品) == true)
            {
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show("保存失败");
            }
            this.Close();
        }
    }
}
